﻿using StockholmsStad.Data.Entity;
using StockholmsStad.Runtime.Serialization.DataContract;
using StockholmsStad.Runtime.Serialization.DataContract.Enums;
using TimeBlock=StockholmsStad.Data.Entity.TimeBlock;
using InformationText=StockholmsStad.Runtime.Serialization.DataContract.InformationText;
using System;

namespace StockholmsStad.Data.ContractFactory
{
    /// <summary>
    /// Konvertering av diverse olika underhållsobjekt till kontraktsobjekt och vice versa
    /// </summary>
    internal class MaintenanceFactory
    {
        public TimeBlock CreateEntityObject(MinisterTimeBlock timeBlock)
        {
            var entityTimeBlock = new TimeBlock
                                      {
                                          StartDateTime = timeBlock.StartDateTime,
                                          IntervalMinutes = timeBlock.Interval,
                                          TimeBlockStatus =
                                              timeBlock.TimeBlockStatus == TimeBlockStatus.Bookable ? 1 : 2,
                                      };

            if (timeBlock.TimeBlockId.HasValue)
                entityTimeBlock.TimeBlockId = timeBlock.TimeBlockId.Value;

            if (timeBlock.Minister != null)
                entityTimeBlock.MinisterId = timeBlock.Minister.MinisterId;

            if (timeBlock.TempMinister != null)
                entityTimeBlock.TempMinisterId = timeBlock.TempMinister.MinisterId;

            return entityTimeBlock;
        }

        public MinisterTimeBlock CreateContractObject(TimeBlock timeBlock)
        {
            var contractTimeBlock = new MinisterTimeBlock
                                        {
                                            StartDateTime = timeBlock.StartDateTime,
                                            Interval = timeBlock.IntervalMinutes,
                                            TimeBlockStatus =
                                                timeBlock.TimeBlockStatus == 1
                                                    ? TimeBlockStatus.Bookable
                                                    : TimeBlockStatus.Break,
                                            TimeBlockId = timeBlock.TimeBlockId,
                                        };

            var ministerFactory = new MinisterFactory();

            if (timeBlock.Minister != null)
                contractTimeBlock.Minister = ministerFactory.CreateContractObject(timeBlock.Minister);

            if (timeBlock.TempMinister != null)
                contractTimeBlock.TempMinister = ministerFactory.CreateContractObject(timeBlock.TempMinister);

            return contractTimeBlock;
        }

        public MarriageCertificate CreateContractObject(ReportMarriageCertificate marriageCertificate)
        {
            var contractCertificate = new MarriageCertificate
                                          {
                                              Language = new ValueIdPair
                                                             {
                                                                 Name = marriageCertificate.Language.Name,
                                                                 Id = marriageCertificate.Language.LanguageId
                                                             },
                                              MarriageCertificateId = marriageCertificate.MarriageCertificateID,
                                              MarriageCertificateType = MarriageCertificateType.Wedding, //Ny lag from 2009-05-01
                                              ReportDefinition = CreateContractObject(marriageCertificate.ReportTextFieldDefinition),
                                          };

            return contractCertificate;
        }

        public ReportTextFieldDefinition CreateEntityObject(ReportDefinition reportDefinition)
        {
            var entityReport = new ReportTextFieldDefinition();
            UpdateEntityObject(entityReport, reportDefinition);
            return entityReport;
        }

        public void UpdateEntityObject(ReportTextFieldDefinition entityReport, ReportDefinition reportDefinition)
        {
            entityReport.Heading = reportDefinition.Heading;
            entityReport.AboveDate = reportDefinition.AboveDate;
            entityReport.BelowSignature = reportDefinition.BelowSignature;
            entityReport.BelowDate = reportDefinition.BelowDate;
            entityReport.BetweenNames = reportDefinition.BetweenNames;
            entityReport.City = reportDefinition.City;
            entityReport.Inception = reportDefinition.Inception;
            entityReport.SignLine = reportDefinition.SignLine;

            if (reportDefinition.ReportDefinitionId.HasValue)
                entityReport.ReportTextFieldDefinitionID = reportDefinition.ReportDefinitionId.Value;
        }

        public void UpdateEntityObject(MinisterTimeBlock ministerTimeBlock, TimeBlock timeBlock)
        {
            if (ministerTimeBlock.Minister != null)
                timeBlock.MinisterId = ministerTimeBlock.Minister.MinisterId;

            if (ministerTimeBlock.TempMinister != null)
                timeBlock.TempMinisterId = ministerTimeBlock.TempMinister.MinisterId;

            timeBlock.IntervalMinutes = ministerTimeBlock.Interval;
            timeBlock.StartDateTime = ministerTimeBlock.StartDateTime;
            timeBlock.TimeBlockStatus = ministerTimeBlock.TimeBlockStatus == TimeBlockStatus.Bookable ? 1 : 2;
        }

        public void UpdateEntityObject(InformationText text, Entity.InformationText entity)
        {
            entity.Header = text.Header;
            entity.Content = text.Content;
        }

        public ValueIdPair CreateContractTextObject(Entity.InformationTextType text)
        {
            return new ValueIdPair {Id = text.InformationTextTypeId, Name = text.Name};
        }


        public InformationText CreateContractTextObject(Entity.InformationText text)
        {
            return new InformationText
                       {
                           TextId = text.InformationTextId,
                           Content = text.Content,
                           Header = text.Header,
                           TextType =
                               new ValueIdPair {Name = text.InformationTextType.Name, Id = text.InformationTextType.InformationTextTypeId},
                       };
        }

        public Entity.InformationText CreateEntityTextObject(InformationText text, int textTypeId)
        {
            var entityText = new Entity.InformationText
                                 {
                Content = text.Content,
                Header = text.Header,
                TextTypeId = textTypeId,
            };

            if (text.TextId.HasValue)
                entityText.InformationTextId = text.TextId.Value;

            return entityText;
        }

        private static ReportDefinition CreateContractObject(ReportTextFieldDefinition textFieldDefinition)
        {
            return new ReportDefinition
            {
                AboveDate = textFieldDefinition.AboveDate,
                BelowDate = textFieldDefinition.BelowDate,
                BelowSignature = textFieldDefinition.BelowSignature,
                BetweenNames = textFieldDefinition.BetweenNames,
                City = textFieldDefinition.City,
                Heading = textFieldDefinition.Heading,
                Inception = textFieldDefinition.Inception,
                ReportDefinitionId = textFieldDefinition.ReportTextFieldDefinitionID,
                SignLine = textFieldDefinition.SignLine,
            };
        }
    }
}